package com.scidpro.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;


public class LobLoader implements Serializable {

	private static final long serialVersionUID = -3584349381256687775L;

	public static abstract class LoaderInterface {

		public abstract void onPopulate(ArrayList<Book> data, boolean isLoading);

	}

	private static String LOB_PATH = "http://www.enpessant.com/lob";

	private transient LoaderInterface loaderInterface;

	public LoaderInterface getLoaderInterface() {
		return loaderInterface;
	}

	public void setLoaderInterface(LoaderInterface loaderInterface) {
		this.loaderInterface = loaderInterface;
	}

	private boolean isLoading;

	private ArrayList<Book> data;

	public LobLoader() {
	}

	public void load(final Context context) {
		isLoading = true;
		populate();

		AsyncTask<Void, Void, ArrayList<Book>> task = new AsyncTask<Void, Void, ArrayList<Book>>() {
			@Override
			protected ArrayList<Book> doInBackground(Void... params) {
				String data = "";
				HttpClient httpclient = new DefaultHttpClient();
				HttpGet request = new HttpGet(LOB_PATH);
				int status = 0;
				try {
					HttpResponse response = httpclient.execute(request);
					status = response.getStatusLine().getStatusCode();
					if (status != HttpStatus.SC_OK) {
						ByteArrayOutputStream ostream = new ByteArrayOutputStream();
						response.getEntity().writeTo(ostream);
						data = ostream.toString();

						ArrayList<Book> bookList = MyDb.getLob(context);

						return bookList;
					} else {
						BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
						StringBuffer stringBuffer = new StringBuffer();
						String line = null;
						while ((line = reader.readLine()) != null) {
							stringBuffer.append(line + "\n");
						}
						data = stringBuffer.toString();
						reader.close();

						ArrayList<Book> bookList = BooksJsonParser.convertJsonToBooksList(data);

						MyDb.putLob(context, data);

						return bookList;
					}
				} catch (IOException e1) {
					Log.e("SCID", e1.getMessage(), e1);
					data = e1.getMessage();

					ArrayList<Book> bookList = MyDb.getLob(context);

					return bookList;
				}
			}

			@Override
			protected void onPostExecute(ArrayList<Book> res) {
				data = res;
				isLoading = false;
				populate();
			}
		};
		task.execute();
	}

	public void populate() {
		if (loaderInterface != null) {
			loaderInterface.onPopulate(data, isLoading);
		}
	}

}
